package com.example.backendmanage.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RedisService {

    private final RedisTemplate<String, String> redisTemplate;
    private final HashOperations<String, String, String> hashOperations;

    @Autowired
    public RedisService(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
        this.hashOperations = redisTemplate.opsForHash();
    }

    public void saveCode(String key, String id, String code) {
        hashOperations.put(key, id, code);
        // 如果需要设置过期时间，可以添加以下语句, 300s 即5分钟
         redisTemplate.expire(key, 300, TimeUnit.SECONDS);
    }

    public String getCodeById(String key, String id) {
        return hashOperations.get(key, id);
    }
}